package com.seari.custody.service.Impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.google.common.collect.Maps;
import com.seari.custody.bean.OutlineProject;
import com.seari.custody.bean.Project;
import com.seari.custody.bean.ProjectExample;
import com.seari.custody.bean.ProjectSearchDTO;
import com.seari.custody.exception.BIZException;
import com.seari.custody.mapper.ProjectMapper;
import com.seari.custody.mapper.TechnicalAdviceMapper;
import com.seari.custody.mapper.TechnicalAuditOpinionMapper;
import com.seari.custody.service.OutLineProjectService;
import com.seari.custody.service.ProjectService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author lxv1
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class ProjectServiceImpl implements ProjectService {
    public static final Map<String, String> maps = Maps.newHashMap();


    {
        maps.put("1", "基坑");
        maps.put("2", "拆除");
        maps.put("3", "勘探");
        maps.put("4", "绿化");
        maps.put("5", "市政");
        maps.put("6", "穿越");
        maps.put("7", "建筑");
        maps.put("8", "加卸载、管线、桥梁、桩基及其他类");
    }
    @Autowired
    private TechnicalAdviceMapper technicalAdviceMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private TechnicalAuditOpinionMapper technicalAuditOpinionMapper;
    @Autowired
    private OutLineProjectService outLineProjectService;

    @Override
    public Project qryByProjectId(String id) {
        return projectMapper.selectByPrimaryKey(id);
    }

    @Override
    public Integer updProject(Project project) {
        return projectMapper.updateByPrimaryKeySelective(project);
    }

    @Override
    public Project qryByProjectNo(String projectNo) {
        ProjectExample projectExample = new ProjectExample();
        projectExample.createCriteria().andNoEqualTo(projectNo);
        List<Project> projects = projectMapper.selectByExample(projectExample);
        if (projects.size() > 0) {
            return projects.get(0);
        }
        return null;
    }

    @Override
    public List<ProjectSearchDTO> qryLikeProjectName(String keyWord) {

        List<ProjectSearchDTO> projectSearchDTOS = projectMapper.qryLikeProjectName(keyWord);
        if (CollectionUtil.isEmpty(projectSearchDTOS)) {
            projectSearchDTOS.add(ProjectSearchDTO.builder().projectName(keyWord).projectId("").projectType("").tip("此项目不存在,点击进行新建").build());
        }
        for (ProjectSearchDTO projectSearchDTO : projectSearchDTOS) {
            projectSearchDTO.setProjectType(maps.get(projectSearchDTO.getProjectType()));
        }
        return projectSearchDTOS;
    }

    @Override
    public Integer createProjectByProjectName(String projectName, String projectId) {
        Project project = new Project();
        project.setProjectName(projectName);
        project.setUuid(projectId);
        return projectMapper.insert(project);

    }

    @Override
    public String getProjectBaseInfoByProjectId(String projectId) {

        Project project = projectMapper.selectByPrimaryKey(projectId);
        if (Objects.nonNull(project)) {
            String uuid = project.getUuid();
            return technicalAuditOpinionMapper.getBaseProjectInfoFile(uuid);
        } else {
            throw new BIZException("获取工程信息概况文档失败");
        }

    }

    @Override
    public Map<String, String> dynamicSelectCard(String projectId) {
        CountDownLatch countDownLatch = new CountDownLatch(3);
        HashMap<String, String> result = Maps.newHashMap();

        try {
            ThreadUtil.execAsync(() -> {
                try {
                    Integer a = technicalAdviceMapper.countTechnicalAdvice(projectId);
                    result.put("techAuditOpinion", a == 0 ? "0" : "1");
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                } finally {
                    countDownLatch.countDown();
                }
            });
            ThreadUtil.execAsync(() -> {
                try {
                    Integer integer = technicalAdviceMapper.countspecialProtection(projectId);
                    result.put("specialProtectionPlan",integer == 0 ? "0" : "1");
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                } finally {
                    countDownLatch.countDown();
                }
            });
            ThreadUtil.execAsync(() -> {
                try {
                    Integer count = outLineProjectService.countByProjectId(projectId);
                    result.put("outlineProject",count==0?"0":"1");
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                } finally {
                    countDownLatch.countDown();
                }
            });

            countDownLatch.await(30, TimeUnit.SECONDS);
        } catch (
                Exception e) {
            throw new BIZException(e);
        }
        return result;
    }

    @Override
    public Project qryByNo(String no) {
        ProjectExample projectExample = new ProjectExample();
        projectExample.createCriteria().andNoEqualTo(no);
        List<Project> projects = projectMapper.selectByExample(projectExample);

        return projects.get(0);
    }


}
